package cn.ljx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ljx.entity.JsonReturn;
import cn.ljx.entity.TBEvaluate;
import cn.ljx.entity.TBHandle;
import cn.ljx.entity.TBJob;
import cn.ljx.entity.TBJobId;
import cn.ljx.entity.TBProblemType;
import cn.ljx.entity.TBUser;
import cn.ljx.service.TBEvaluateService;
import cn.ljx.service.TBJobService;
import cn.ljx.service.TBProblemTypeService;
import cn.ljx.service.TBjobidService;
import cn.ljx.util.BaseUtils;

@Controller
@RequestMapping("/Job")
public class JobController {
	@Autowired
    private TBjobidService tbjobidService;
	@Autowired
	private TBJobService tbjobService;
	@Autowired
	private TBEvaluateService tbevaluateService;

	@Autowired
	private TBProblemTypeService tbproblemService;
	//新增工单
	@ResponseBody
	@RequestMapping(value="/addJob",method=RequestMethod.POST)
	public JsonReturn addJob(TBJob tbjob){
		JsonReturn jr = new JsonReturn();  //Json格式
		String problem = tbjob.getProblem();
		TBProblemType tbProblemType = tbproblemService.getProblemByName(problem); //获得故障类型详情
		try {
			tbjob.setId(BaseUtils.getUUID32()); //设置ID位32UUID
			tbjob.setCreate_time(BaseUtils.getTime()); // 设置时间为当前系统时间
			tbjob.setJob_id(getJobId());
			tbjob.setState("1");
			tbjob.setIs_lock("0");
			tbjob.setJob_state("待接单");
			tbjob.setStandard_time(tbProblemType.getJobtime());
			int i = tbjobService.addJob(tbjob);
			//判断是否新增工单成功
			if(i!=0){
				jr.setInfo("success");  
			}else{
				jr.setInfo("false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jr;
	}

	//获得工单id
	public String getJobId(){
		StringBuffer jobId =new StringBuffer();
		TBJobId jobid = tbjobidService.getJobid(); //数据库获得当前工单数
		String finalJobId ="";
		String updateNum=""; 
		int jobNum = 0;
		try{
			if(jobid!=null){
				jobNum = Integer.parseInt(jobid.getJob_num());//
				if(String.valueOf(jobNum).length()<8){
					//补齐工单，使得工单后缀长度为8
					for(int i=0;i<8-String.valueOf(jobNum).length();i++){
						jobId.append("0");  
					}
				}
			}
			jobId.append(jobNum); 
			jobNum = jobNum +1; //将工单数+1
			updateNum = String.valueOf(jobNum); //更新工单数 
			finalJobId = jobid.getJob_prefix()+jobId.toString(); //最终工单ID
			tbjobidService.updateJobid(updateNum, jobid.getId());//更新工单数到数据库
		}catch (Exception e){
			e.printStackTrace();
		}
		return finalJobId;
	}
	
	//抢单
	@ResponseBody
	@RequestMapping(value="/grapJob",method=RequestMethod.POST)
	public JsonReturn grapJob(TBJob tbjob,Model model,HttpSession httpsession){
		TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
		TBJob job = tbjobService.getJobById(tbjob.getJob_id());
		JsonReturn jr = new JsonReturn();
		if(job.getIs_lock()!=null&&job.getIs_lock().equals("1")){
			jr.setInfo("false");
			return jr;
		}
		try {
			tbjob.setJob_state("已接单");
			tbjob.setDeal_name(userSession.getReal_name());
			tbjob.setDeal_phone(userSession.getPhone());
			tbjob.setDeal_time(BaseUtils.getTime());
			tbjob.setIs_lock("1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = tbjobService.grapJob(tbjob);
		if(i==1){
			jr.setInfo("success");
		}
		return jr;
	}
	
	//完结工单
	@ResponseBody
	@RequestMapping(value="/finishJob",method=RequestMethod.POST)
	public JsonReturn finishJob(TBJob tbjob){
		JsonReturn jr = new JsonReturn();
		int i= 0;
		try {
			tbjob.setJob_state("完成");
			tbjob.setFinish_time(BaseUtils.getTime());
			i = tbjobService.finishJob(tbjob);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(i!=0){
			jr.setInfo("success");
		}
		return jr;
	}
	
	//工单池列表
	@ResponseBody
	@RequestMapping(value="/jobPool",method=RequestMethod.POST)
	public JsonReturn jobPool(){
		JsonReturn jr = new JsonReturn();
		List<TBJob> tbjob = tbjobService.jobPool();
		if(tbjob.size()>0){
			jr.setInfo("success");
			jr.setObject(tbjob);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//工单详情
	@ResponseBody
	@RequestMapping(value="/getJobById",method=RequestMethod.POST)
	public JsonReturn getJobById(String job_id){
		JsonReturn jr = new JsonReturn();
		TBJob tbjob = tbjobService.getJobById(job_id);
		if(tbjob!=null){
			jr.setInfo("success");
			jr.setObject(tbjob);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//工单列表
	@ResponseBody
	@RequestMapping(value="/getjobList",method=RequestMethod.POST)
	public JsonReturn getjobList(String job_id){
		JsonReturn jr = new JsonReturn();
		List<TBJob> tbjob = tbjobService.getjobList(job_id);
		if(tbjob!=null){
			jr.setInfo("success");
			jr.setObject(tbjob);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//工单列表
	@ResponseBody
	@RequestMapping(value="/jobhistory",method=RequestMethod.POST)
	public JsonReturn jobhistory(String job_id,Model model,HttpSession httpsession){
		Map<String,Object> map = new HashMap<String,Object>();
		JsonReturn jr = new JsonReturn();
		TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
		map.put("job_id", job_id);
		map.put("create_name", userSession.getReal_name());
		List<TBJob> tbjob = tbjobService.jobhistory(map);
		if(tbjob!=null){
			jr.setInfo("success");
			jr.setObject(tbjob);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	
	//工单加评价
	@ResponseBody
	@RequestMapping(value="/getjoball",method=RequestMethod.POST)
	public JsonReturn getjoball(String job_id){
		JsonReturn jr = new JsonReturn();
		Map<String,Object> map = new HashMap<String,Object>();
		TBJob tbjob = tbjobService.getJobById(job_id);
		TBEvaluate tbEvaluate = tbevaluateService.getEvaluateById(job_id);
		map.put("detail", tbjob);
		map.put("evaluate", tbEvaluate);
		if(map.size()>0){
			jr.setInfo("success");
			jr.setObject(map);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//工单删除
	@ResponseBody
	@RequestMapping(value="/delectJob",method=RequestMethod.POST)
	public JsonReturn delectJob(String job_id){
		JsonReturn jr = new JsonReturn();
		int i = tbjobService.delectJob(job_id);
		tbevaluateService.deteleevaluate(job_id);
		if(i!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//未评价工单
	@ResponseBody
	@RequestMapping(value="/doingEvaluate",method=RequestMethod.POST)
	public JsonReturn doingEvaluate(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
		if(userSession!=null){
		}else{
			jr.setInfo("null");
			return jr;
		}
		List<TBJob> tbjoblist = tbjobService.doingEvaluate(userSession.getReal_name());
		if(tbjoblist.size()>0){
			jr.setInfo("success");
			jr.setObject(tbjoblist);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
	
	//评价工单
	@ResponseBody
	@RequestMapping(value="/evaluateJob",method=RequestMethod.POST)
	public JsonReturn evaluateJob(TBEvaluate tbEvaluate){
		JsonReturn jr = new JsonReturn();
		int i = tbjobService.evaluateJob(tbEvaluate.getJob_id());
		TBJob tbJob = tbjobService.getJobById(tbEvaluate.getJob_id());
		try {
			tbEvaluate.setId(BaseUtils.getUUID32());
			tbEvaluate.setCreate_time(BaseUtils.getTime());
			tbEvaluate.setCreate_name(tbJob.getCreate_name());
			tbEvaluate.setHandle_name(tbJob.getDeal_name());
			tbEvaluate.setState("1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int j = tbevaluateService.addEvaluate(tbEvaluate);
		if(i!=0&&j!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value="/countnum",method=RequestMethod.POST)
	public JsonReturn countnum(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		Map<String,Object> map = new HashMap<String,Object>(); 
		TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
		if(userSession!=null){
		}else{
			jr.setInfo("null");
			return jr;
		}
		int finishcount = tbjobService.finishcount(userSession.getReal_name());
		int doingcount = tbjobService.doingcount(userSession.getReal_name());
		int timecount = tbjobService.timecount(userSession.getReal_name());
		int badevaluate = tbevaluateService.countbad(userSession.getReal_name());
		/*int finaltime = timecount-badevaluate;*/
		map.put("finishcount", finishcount);
		map.put("doingcount", doingcount);
		map.put("timecount", timecount);
		if(map.size()>0){
			jr.setInfo("success");
			jr.setObject(map);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
	
	//未完成工单
	@ResponseBody
	@RequestMapping(value="/doingJob",method=RequestMethod.POST)
	public JsonReturn doingJob(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
		if(userSession!=null){
		}else{
			jr.setInfo("null");
			return jr;
		}
		List<TBJob> tbjoblist = tbjobService.doingJob(userSession.getReal_name());
		if(tbjoblist.size()>0){
			jr.setInfo("success");
			jr.setObject(tbjoblist);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
	
	//未完成工单
	@ResponseBody
	@RequestMapping(value="/finishallJob",method=RequestMethod.POST)
	public JsonReturn finishallJob(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBHandle userSession = (TBHandle) httpsession.getAttribute("tbhandle");
		if(userSession!=null){
		}else{
			jr.setInfo("null");
			return jr;
		}
		List<TBJob> tbjoblist = tbjobService.finishallJob(userSession.getReal_name());
		if(tbjoblist.size()>0){
			jr.setInfo("success");
			jr.setObject(tbjoblist);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value="/countall",method=RequestMethod.POST)
	public JsonReturn countall(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		Map<String,Object> map = new HashMap<String,Object>(); 
		int goodcount = tbevaluateService.countallgood();
		int mediumcount = tbevaluateService.countallmedium();
		int badcount = tbevaluateService.countallbad();
		map.put("goodcount", goodcount);
		map.put("mediumcount", mediumcount);
		map.put("badcount", badcount);
		if(map.size()>0){
			jr.setInfo("success");
			jr.setObject(map);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
	
	
		
		@ResponseBody
		@RequestMapping(value="/countjoball",method=RequestMethod.POST)
		public JsonReturn countjoball(Model model,HttpSession httpsession){
			JsonReturn jr = new JsonReturn();
			Map<String,Object> map = new HashMap<String,Object>(); 
			int finishcount = tbjobService.finishallcount();
			int doingcount = tbjobService.doingallcount();
			map.put("finishcount", finishcount);
			map.put("doingcount", doingcount);
			if(map.size()>0){
				jr.setInfo("success");
				jr.setObject(map);
			}else{
				jr.setInfo("null");
			}
			return jr;
		}
		
	//个人统计
	@ResponseBody
	@RequestMapping(value="/countperple",method=RequestMethod.POST)
	public JsonReturn countperple(String deal_name){
		JsonReturn jr = new JsonReturn();
		List<Map<String,Object>> list = tbjobService.countperple(deal_name);
		if(list!=null){
			jr.setInfo("success");
			jr.setMaplist(list);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//总体统计
	@ResponseBody
	@RequestMapping(value="/countype",method=RequestMethod.POST)
	public JsonReturn countype(String problem){
		JsonReturn jr = new JsonReturn();
		List<Map<String,Object>> list = tbjobService.countype(problem);
		if(list!=null){
			jr.setInfo("success");
			jr.setMaplist(list);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	
	//未评价工单
	@ResponseBody
	@RequestMapping(value="/cancelList",method=RequestMethod.POST)
	public JsonReturn cancelList(Model model,HttpSession httpsession){
		JsonReturn jr = new JsonReturn();
		TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
		List<TBJob> tbjoblist = tbjobService.cancelList(userSession.getReal_name());
		if(tbjoblist.size()>0){
			jr.setInfo("success");
			jr.setObject(tbjoblist);
		}else{
			jr.setInfo("null");
		}
		return jr;
	}
}
